﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShow.DAL;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShow.BLL
{
    public class MemberService : BaseService<User>
    {
        #region 会员管理-删除
        public bool DeleteUser(User user)
        {
            using (var db = new CrookedShowEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        Expression<Func<User_Address, bool>> lambdaExpression = a => a.UserId == user.Id;
                        var t = db.User_Address.Where(lambdaExpression);
                        List<User_Address> list = t.ToList();
                        for (int i = 0; i < list.Count; i++)
                        {
                            db.User_Address.Remove(list[i]);
                        }
                        //删除主表
                        db.Entry(user).State = EntityState.Deleted;
                        db.SaveChanges();
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return false;
                        throw;
                    }
                }
            }
            return true;
        } 
        #endregion
    }
}
